using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.MP.AdvancedAPIs.QrCode;
using Senparc.Weixin.MP.CommonAPIs;
using System;
using System.IO;
namespace Senparc.Weixin.MP.AdvancedAPIs
{
	public static class QrCodeApi
	{
		public static CreateQrCodeResult Create(string accessTokenOrAppId, int expireSeconds, int sceneId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<CreateQrCodeResult>(delegate(string accessToken)
			{
				string urlFormat = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}";
				object data;
				if (expireSeconds > 0)
				{
					data = new
					{
						expire_seconds = expireSeconds,
						action_name = "QR_SCENE",
						action_info = new
						{
							scene = new
							{
								scene_id = sceneId
							}
						}
					};
				}
				else
				{
					data = new
					{
						action_name = "QR_LIMIT_SCENE",
						action_info = new
						{
							scene = new
							{
								scene_id = sceneId
							}
						}
					};
				}
				return CommonJsonSend.Send<CreateQrCodeResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static CreateQrCodeResult CreateByStr(string accessTokenOrAppId, string sceneStr, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<CreateQrCodeResult>(delegate(string accessToken)
			{
				string urlFormat = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}";
				var data = new
				{
					action_name = "QR_LIMIT_STR_SCENE",
					action_info = new
					{
						scene = new
						{
							scene_str = sceneStr
						}
					}
				};
				return CommonJsonSend.Send<CreateQrCodeResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static string GetShowQrCodeUrl(string ticket)
		{
			string format = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0}";
			return string.Format(format, ticket);
		}
		public static void ShowQrCode(string ticket, Stream stream)
		{
			string showQrCodeUrl = QrCodeApi.GetShowQrCodeUrl(ticket);
			Get.Download(showQrCodeUrl, stream);
		}
	}
}
